package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.ProductRemainingDAO;
import org.millioncall.yueyoga.admin.model.ProductRemaining;
import org.millioncall.yueyoga.admin.model.ProductUsedInfo;

public class ProductRemainingDAOImpl extends BaseDao implements ProductRemainingDAO {

	@Override
	public int save(ProductRemaining productRemaining) {
		return this.getPagableSqlSessionTemplate().insert("saveProductRemaining", productRemaining);
	}

	@Override
	public int update(Map<String, Object> map) {
		if (map.get("counts") != null) {
			return this.getPagableSqlSessionTemplate().update("updateCounts", map);
		} else {
			return this.getPagableSqlSessionTemplate().update("updateTime", map);
		}
	}

	@Override
	public void deleteByMemberIds(Integer[] deids) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < deids.length; i++) {
			map.put("id", deids[i]);
			this.getPagableSqlSessionTemplate().insert("deleteProductRemainingBymemberId", map);
		}
	}

	@Override
	public PaginatedList<ProductRemaining> findPage(int start, int limit, Map<String, Object> map) {
		PreparedQuery<ProductRemaining> pq = this.getPagableSqlSessionTemplate().findAll("findProductRemainingPages", map);
		PaginatedList<ProductRemaining> pl = pq.list(new PaginateParams(new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public int getCountsByMembetId(Map<String, Object> map) {
		Map<String, Object> conuts = this.getPagableSqlSessionTemplate().selectOne("getCountByMemberId", map);
		if (conuts == null) {
			return 0;
		} else {
			return Integer.parseInt(conuts.get("count").toString());
		}
	}

	@Override
	public void deleteByIds(Integer[] deids) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < deids.length; i++) {
			map.put("id", deids[i]);
			this.getPagableSqlSessionTemplate().insert("deleteProductRemaining", map);
		}
	}

	@Override
	public PaginatedList<ProductRemaining> findMemberProduct(int start, int limit, Map<String, Object> map) {

		PreparedQuery<ProductRemaining> pq = this.getPagableSqlSessionTemplate().findAll("findMemberProductsByMemberId", map);
		PaginatedList<ProductRemaining> pl = pq.list(new PaginateParams(new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public PaginatedList<ProductRemaining> findProductByMemberId(Integer start, Integer limit, Integer memberId) {
		PreparedQuery<ProductRemaining> pq = this.getPagableSqlSessionTemplate().findAll("findProductByMemberId", memberId);
		PaginatedList<ProductRemaining> pl = pq.list(new PaginateParams(new StartLimitPairImpl(start, limit)));
		return pl;
	}

	/***
	 * 查询产品的使用情况
	 */
	@Override
	public PaginatedList<ProductUsedInfo> findMemberProductsUsed(int start, int limit, Map<String, Object> map) {
		PreparedQuery<ProductUsedInfo> pq = this.getPagableSqlSessionTemplate().findAll("findMemberProductsByMemberInfo", map);
		PaginatedList<ProductUsedInfo> pl = pq.list(new PaginateParams(new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public ProductRemaining findProductRemainingById(int proRemainId) {
		return this.getPagableSqlSessionTemplate().selectOne("findProductRemainingById", proRemainId);
	}

	@Override
	public int updateProductRemaining(ProductRemaining productRemaining) {
		return this.getPagableSqlSessionTemplate().update("updateProductRemaining", productRemaining);
	}

	@Override
	public boolean saveClearOrder(ProductRemaining pr) {
		int i = this.getPagableSqlSessionTemplate().insert("saveProductRemaining", pr);
		if (i > 0)
			return true;
		else
			return false;
	}

	@Override
	public List<ProductRemaining> findMemberProductById(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.getPagableSqlSessionTemplate().selectList("findMemberProductsByMemberId", map);
	}

	@Override
	public PaginatedList<ProductRemaining> findConsumptionProductRemaining(Map<String, Object> map, Integer start, Integer limit) {
		PreparedQuery<ProductRemaining> pq = this.getPagableSqlSessionTemplate().findAll("findConsumptionProductRemaining", map);
		PaginatedList<ProductRemaining> pl = pq.list(new PaginateParams(new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public boolean deleteByIds(String Ids) {
		int i = this.getPagableSqlSessionTemplate().delete("deleteProductRemainingByIds", Ids);
		if (i > 0)
			return true;
		return false;
	}

	@Override
	public int updateCounts(Integer prId, Integer counts) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("prId", prId);
		map.put("counts", counts);
		return this.getPagableSqlSessionTemplate().update("updatePRCounts", map);
	}

	@Override
	public ProductRemaining findProductRemainingByHyCpId(Map<String, Object> map) {
		PreparedQuery<ProductRemaining> pq = this.getPagableSqlSessionTemplate().findAll("findProductRemainingByHyCpId", map);
		return pq.uniqueResult();
	}
}
